<template>
    
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片、 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input
            placeholder="请输入内容"
            v-model="querInfo.query"
            clearable
            @clear="getUserList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getUserList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="dialogVisible = true"
            >添加用户</el-button
          >
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="tableData" stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column
          prop="username"
          label="姓名"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="email"
          label="邮箱"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="mobile"
          label="电话"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="role_name"
          label="角色"
          width="180"
        ></el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @change="userStateChange(scope.row)"
              active-color="#409EFF"
              inactive-color="#DCDFE6"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <!-- 编辑 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="edit(scope.row.id)"
            ></el-button>
            <!-- 删除 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="remove(scope.row.id)"
            ></el-button>
            <!-- 权限 -->
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="querInfo.pagenum"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="querInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加弹出框 -->
    <el-dialog
      title="添加用户"
      :visible.sync="dialogVisible"
      width="40%"
      @close="addDialogClosed"
    >
      <el-form :model="add" :rules="addRules" ref="addRef" label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="add.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="add.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="add.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="add.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改弹框 -->
    <el-dialog title="提示" :visible.sync="dialogEdit" width="40%">
      <el-form
        :model="editform"
        :rules="addRules"
        ref="addRef"
        label-width="70px"
      >
        <el-form-item label="用户名">
          <el-input v-model="editform.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editform.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="mobile">
          <el-input v-model="editform.mobile"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogEdit = false">取 消</el-button>
        <el-button type="primary" @click="editOk">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  users,
  states,
  add,
  remove,
  editAdd,
  beforeEdit,
} from "../../api/user";
export default {
  name: "",
  data() {
    return {
      input: "",
      tableData: [],
      querInfo: {
        query: "",
        pagenum: 1,
        pagesize: 2,
      },
      total: 0,
      dialogVisible: false,
      dialogEdit: false,
      add: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
      editform: {},
      addRules: {
        username: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: "请输入邮箱",
            trigger: "blur",
          },
        ],
        mobile: [
          {
            required: true,
            message: "请输入电话",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getUserList();
  },
  computed: {},
  methods: {
    async getUserList() {
      const { data: res } = await users(this.querInfo);
      //   console.log(res);
      this.tableData = res.data.users;
      this.total = res.data.total;
    },
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`);
      this.querInfo.pagesize = val;
      this.getUserList();
    },
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`);
      this.querInfo.pagenum = val;
      this.getUserList();
    },
    async userStateChange(userinfo) {
      // console.log(userinfo);
      const { data: res } = await states(userinfo.id, userinfo.mg_state);
      //   console.log(res);
      if (res.meta.status !== 200) {
        userinfo.mg_state = !userinfo.mg_state;
        return this.$message.error("修改用户状态失败");
      }
      this.$message.success("修改用户状态成功");
    },
    // 添加用户
    addDialogClosed() {
      this.$refs.addRef.resetFields();
    },
    addUser() {
      this.$refs.addRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await add(this.add);
        console.log(res);
        if (res.meta.status == 201) {
          this.$message.success(res.meta.msg);
        } else {
          this.$message.error(res.meta.msg);
        }
        this.dialogVisible = false;
        this.getUserList();
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 删除
    async remove(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          const { data: res } = await remove(id);
          console.log(res);
          this.getUserList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 编辑
    async edit(id) {
      this.dialogEdit = true;
      const { data: res } = await beforeEdit(id);
      console.log(res);
      this.editform = res.data;
    },
    editOk() {
      this.$refs.addRef.validate(async (valid) => {
        if (!valid) return;
        const { data: res } = await editAdd(this.editform.id, {
          email: this.editform.email,
          mobile: this.editform.mobile,
        });
        console.log(res);
        if (res.meta.status !== 200) return this.$message.error("修改失败");
        this.$message.success("修改成功");
        this.dialogEdit = false;
        this.getUserList();
      });
    },
  },
};
</script>

<style scoped>
</style>
